#!/bin/bash
# $XConsortium: Xsession /main/10 1995/12/18 18:21:28 gildea $
#
#  Special custom for Archlive
#  Carbon Jiao <http://archlive.googlecode.com>
#
# $XFree86: xc/programs/xdm/config/Xsession,v 1.2 1998/01/11 03:48:32 dawes Exp $
# Modified for Slackware-3.5, 28-Mar-98 volkerdi
# Extensively rewritten for Slackware 7.0, 03-Oct-1999 volkerdi
# Patched to give priority to $HOME/.xsession, 10-Oct-1999 volkerdi
# Merged changes into upstream (XFree86-4.0.2) version, 17-Feb-2001 volkerdi
# Fixes for $PATH (from Jim Diamond), GDM/KDM/XDM, 2003-02-07 volkerdi
# Patched for bizzare GDM $1, 2003-09-23 volkerdi
# Heavily modified by Jean-Philippe Guillemin for Zenwalk Linux - Wed Jul  4 19:50:38 CEST 2007


# redirect errors to a file in user's home directory if we can
for errfile in "$HOME/.xsession-errors" "${TMPDIR-/tmp}/xses-$USER" "/tmp/xses-$USER"
do
	if ( cp /dev/null "$errfile" 2> /dev/null ) ; then
		chmod 600 "$errfile"
		exec > "$errfile" 2>&1
		break
	fi
done

# fix broken $UID on some system...
if test "x$UID" = "x"; then
	if test -x /usr/xpg4/bin/id; then
		UID=`/usr/xpg4/bin/id -u`;
	else
		UID=`id -u`;
	fi
fi

SESSION="$1"
SESSIONS="/etc/X11/sessions"

userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/usr/lib/X11/xinit/.Xresources
sysmodmap=/usr/lib/X11/xinit/.Xmodmap

dmrc=/var/state/dm/dmrc
userdmrc=$HOME/.dmrc

# $XDG_CONFIG_HOME defines the base directory relative to which user specific 
# configuration files should be stored. If $XDG_CONFIG_HOME is either not set 
# or empty, a default equal to $HOME/.config should be used.
if test "x$XDG_CONFIG_HOME" = "x" ; then
  XDG_CONFIG_HOME=$HOME/.config
fi
[ -d "$XDG_CONFIG_HOME" ] || mkdir "$XDG_CONFIG_HOME"

# $XDG_CACHE_HOME defines the base directory relative to which user specific 
# non-essential data files should be stored. If $XDG_CACHE_HOME is either not 
# set or empty, a default equal to $HOME/.cache should be used.
if test "x$XDG_CACHE_HOME" = "x" ; then
  XDG_CACHE_HOME=$HOME/.cache
fi
[ -d "$XDG_CACHE_HOME" ] || mkdir "$XDG_CACHE_HOME"


if [ -e $dmrc ]; then
	localecmd="$(grep "LANG=" $dmrc 2>/dev/null)"
	keymapcmd="$(grep "loadkeys" $dmrc 2>/dev/null)"
	xkbmapcmd="$(grep "setxkbmap" $dmrc 2>/dev/null)"
	if [ "$localecmd" ]; then 
		if [ "$(grep "LANG=" $userdmrc 2>/dev/null)" ] ; then
			sed -i "s@^.*LANG=.*@$localecmd@" $userdmrc 
		else
			echo "$localecmd" >> $userdmrc
		fi
	fi
	if [ "$keymapcmd" ]; then 
		if [ "$(grep "loadkeys" $userdmrc 2>/dev/null)" ]; then
			sed -i "s@^.*loadkeys.*@$keymapcmd@" $userdmrc 
		else			
			echo "$keymapcmd" >> $userdmrc
		fi			
	fi	
	if [ "$xkbmapcmd" ]; then 
		if [ "$(grep "setxkbmap" $userdmrc 2>/dev/null)" ]; then
			sed -i "s@^.*setxkbmap.*@$xkbmapcmd@" $userdmrc
		else
			echo "$xkbmapcmd" >> $userdmrc
		fi
	fi		
	
	rm -f $dmrc
fi

# merge in defaults and keymaps

if [ -r $sysresources ]; then
	xrdb -merge $sysresources
fi

if [ -r $sysmodmap ]; then
	xmodmap $sysmodmap
fi

if [ -r $userresources ]; then
	xrdb -merge $userresources
fi

if [ -r $usermodmap ]; then
	xmodmap $usermodmap
fi

# Since xdm doesn't run a bash -login shell (or any other login shell)
# we should source these files to set up the user's environment.
profile=/etc/profile
userprofile=~/.profile

if [ -r $profile ]; then
	source $profile 1> /dev/null 2> /dev/null
fi
if [ -r $userprofile ]; then
	source $userprofile 1> /dev/null 2> /dev/null
fi

# Set the path
if [ "`whoami`" = "root" ]; then
	PATH=$PATH:/usr/local/sbin:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin
else
	PATH=$PATH:/usr/local/bin:/bin:/usr/bin
fi

# These files (if they exist) are used to set up the X related environment.  We used to
# exec .xsession at this location, but that can interfere with choosing a session type
# through XDM/KDM/GDM so it was moved to after a requested session is started.  Since
# that means that .xsession might never be run at all when using XDM/KDM/GDM, support
# for the xprofile was added to allow a way for the user to customize the X environment.
if [ -r /etc/xprofile ]; then
	source /etc/xprofile
fi
if [ -r ~/.xprofile ]; then
	source ~/.xprofile
fi

# Session specific parameters set from the DM
if [ -e $userdmrc ]; then
	source $userdmrc 1> /dev/null 2> /dev/null
fi

# Launch xscreensaver (if available), but only as non-root user
if test $UID -gt 0 -a -z "$VNCSESSION"; then 
    if test x"`which xscreensaver 2>/dev/null`" != x""; then
        xscreensaver -no-splash &
    elif test x"`which gnome-screensaver 2>/dev/null`" != x""; then
        gnome-screensaver &
    fi
fi 

# We should normally get the session command from desktop files
if [ "x${SESSION}" != "x" ]; then
	COMMAND="$(sed -n 's/Exec[ \t]*=[ \t]*\(.*\)$/\1/p' ${SESSIONS}/${SESSION}.desktop)"
	exec $COMMAND
fi

# Else we will try to do it in the old good way...
case "$SESSION" in
failsafe)
	xsetroot -solid SteelBlue
	exec xterm -geometry 80x24-0-0
	;;
xfce)
	exec /etc/X11/xinit/xinitrc.xfce
	;;
lxde)
	exec /etc/X11/xinit/xinitrc.lxde
	;;
lxlauncher)
	exec lxlauncher
	;;
openbox-lxde)
	exec openbox-lxde
	;;
awesome)
	exec /etc/X11/xinit/xinitrc.awesome 
	;;
kde)
	exec startkde
	;;
kde4)
	exec /opt/kdemod/bin/startkde
	;;
gnome)
	exec /usr/bin/gnome-session
	;;
fluxbox)
	exec /etc/X11/xinit/xinitrc.fluxbox
	;;
fvwm2)
	xsetroot -solid SteelBlue
	exec fvwm2
	;;
fvwm95)
	xsetroot -solid SteelBlue
	exec fvwm95
	;;
blackbox)
	exec blackbox
	;;
windowmaker)
	#exec /etc/X11/xinit/xinitrc.wmaker
	exec wmaker
	;;
afterstep)
	exec afterstep
	;;
enlightenment)
	exec starte16
	;;
e17svn)
	exec /etc/X11/xinit/xinitrc.e17
	;;
openbox*)
	exec exec /etc/X11/xinit/xinitrc.openbox
	;;
icewm)
	exec exec /etc/X11/xinit/xinitrc.icewm
	;;
twm)
	#xclock -geometry 50x50-1+1 &
	#xterm -geometry 80x50+494+51 &
	#xterm -geometry 80x20+494-0 &
	#xterm -geometry 80x66+0+0 -name login &
	xsetroot -solid SteelBlue
	exec twm
	;;
*)
	if [ "x$session" != "x' ]; then
		exec /etc/X11/xinit/xinitrc.$session
	elif [ -x $HOME/.xinitrc ]; then
		exec $HOME/.xinitrc
	elif [ -x $HOME/.xsession ]; then
		exec $HOME/.xsession $@
	else
		exec /etc/X11/xinit/xinitrc.openbox
	fi
	;;
esac
